package cc.magicjson.spring.exploration.config;

import cc.magicjson.spring.exploration.advisor.InfrastructureAdvice;
import cc.magicjson.spring.exploration.advisor.RegularAdvice;
import org.springframework.aop.Advisor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.JdkRegexpMethodPointcut;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Role;

import static org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE;

@Configuration // 标记这是一个配置类
public class Config {

    @Bean // 定义一个Bean
    @Role(ROLE_INFRASTRUCTURE) // 标记这个Bean为基础设施角色
    public Advisor infrastructureAdvisor(InfrastructureAdvice advice) {
        // 创建一个基于正则表达式的切入点
        JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
        // 设置切入点匹配的类路径模式
        pointcut.setPattern("cc.magicjson.spring.exploration.service.*");
        // 创建并返回一个Advisor，将切入点和通知组合在一起
        return new DefaultPointcutAdvisor(pointcut, advice);
    }

    @Bean
    public Advisor regularAdvisor() {
        return new DefaultPointcutAdvisor(new RegularAdvice());
    }
}
